package com.liliudong.template.common;

import lombok.Getter;

/**
 * @author ldli5
 * @classname R
 * @date 2024/6/18-13:59
 */
@Getter
public class R<T> {

    private static final String SUCCESS = "success";
    public static final String ERROR = "error";

    private final T data;
    private final String msg;
    private final int code;

    private R(T data, String msg, int code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public static <T> R<T> ok(T data, String msg, int code) {
        return new R<>(data, msg, code);
    }

    public static <T> R<T> ok(T data, String msg) {
        return ok(data, msg, 1);
    }

    public static <T> R<T> ok(T data) {
        return ok(data, SUCCESS);
    }

    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> error(String msg, int code) {
        return new R<>(null, msg, code);
    }

    public static <T> R<T> error(String msg) {
        return error(msg, 0);
    }

    public static <T> R<T> error(int code) {
        return error(ERROR, code);
    }
}
